package com.kkwrite.regimen.provider.controller.share;

import com.kkwrite.regimen.common.entity.dto.InDTO;
import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.share.ShareLogInDTO;
import com.kkwrite.regimen.provider.service.share.ShareService;
import com.kkwrite.regimen.provider.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_FALSE;
import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_TRUE;

/**
 * Created by lisha on 2018/7/19 21:25.
 *
 * @author lisha
 */
@Api(value = "分享模块")
@RestController
public class ShareController {

    private final List<Integer> shopShareLogType = Arrays.asList(1, 2);

    @Resource(name = "shareService")
    private ShareService shareService;
    @Resource(name = "tokenUtil")
    private TokenUtil tokenUtil;

    @PostMapping(value = "/share/saveLog")
    public OutDTO saveShareLog(@RequestBody InDTO<Map<String, String>> inDTO) {
        try {
            shareService.saveLog(inDTO);
        } catch (Exception e) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "服务异常！异常信息：" + e.getMessage());
        }
        return OutDTO.of(BOOLEAN_STRING_TRUE, "OK", "OK");
    }

    @PostMapping(value = "/share/saveshoplog")
    @ApiOperation(value = "保存店铺分享日志")
    @ApiImplicitParam(name = "log", value = "分享日志类型，当分享类型为文章的时候，需要传文章消息的ID（itemId），其他类型itemId请传null",
            required = true, paramType = "body", dataType = "ShareLogInDTO")
    public OutDTO saveShopShareLog(@RequestBody ShareLogInDTO log, @ApiIgnore @RequestHeader String token) {
        String shopIdString = (String) tokenUtil.getValue(token, TokenUtil.TokenKeyEnum.SHOP_ID);
        String empIdString = (String) tokenUtil.getValue(token, TokenUtil.TokenKeyEnum.EMPLOYEE_ID);
        if (shopIdString == null || empIdString == null) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "002", "token invalid");
        }
        if (log == null) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "入参不合法");
        }
        Integer type = log.getType();
        if (!shopShareLogType.contains(type)) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "入参不合法");
        }
        Integer itemId = log.getItemId();
        if (type == 1 && itemId == null) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "入参不合法");
        }
        if (type != 1 && itemId != null) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "入参不合法");
        }
        try {
            shareService.saveShopShareLog(Integer.valueOf(shopIdString), Integer.valueOf(empIdString), type, itemId);
        } catch (Exception e) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "服务异常！异常信息：" + e.getMessage());
        }
        return OutDTO.of(BOOLEAN_STRING_TRUE, "OK", "OK");
    }

}
